﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using AdaptationProject.Properties;
using AdaptationProject.Utils;
using SharedTypes;

namespace AdaptationProject
{
    public partial class AddNewAlgorithm : Form
    {
        private struct ParameterInfo
        {
            public byte order;
            public string paramName;
            public string description;
            public string values;
            public string defaultValue;

            public ParameterInfo(byte order, string parName, string descr, string val, string defValue)
            {
                this.order = order;
                paramName = parName;
                description = descr;
                values = val;
                defaultValue = defValue;
            }
        }

        private const string XmlExt = ".xml";
        private const string DllExt = ".dll";
        private const string ExeExt = ".exe";
        private IList<ParameterInfo> localLst = new List<ParameterInfo>();

        public AddNewAlgorithm()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ParameterInfo param = new ParameterInfo((byte)nudOrderNum.Value, 
                                                    tbParamName.Text, 
                                                    tbDescription.Text, 
                                                    tbAcceptableValues.Text, 
                                                    tbDefaultValue.Text);
            localLst.Add(param);
            lbAddedParameters.Items.Add(param.paramName);
            nudOrderNum.Value++;
            ClearParamTBoxes();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            nudOrderNum.Value = 0;
            localLst.Clear();
            lbAddedParameters.Items.Clear();
            ClearParamTBoxes();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = openFileDialog1.SafeFileName;
                string filePath = openFileDialog1.FileName;
                if (fileName.EndsWith(DllExt))
                {
                    tbDllPath.Text = filePath;
                }
                else
                {
                    MessageBox.Show(Resources.ADDALGDescr_InvalidLibraryExtText, Resources.ADDALGDescr_InvalidLibraryExtTitle,
                                        MessageBoxButtons.OK);
                }
            }
        }

        private void btnFinish_Click(object sender, EventArgs e)
        {
            //VALIDATION OF THIS CODE!
            XMLParser localParset = new XMLParser();
            string path = Path.Combine(MySettings.AlgorithmsCachePath, tbFileName.Text);
            localParset.CreateTemplateDocument(path);
            localParset.Load(path);
            localParset.SetHeader(tbAlgName.Text, tbDllPath.Text, tbExePath.Text);
            foreach (ParameterInfo info in localLst)
            {
                localParset.AddParameter(info.order, info.paramName, info.description, info.values, info.defaultValue);
            }
            localParset.Save();
            localLst.Clear();
            ClearAll();
            Hide();
        }

        private void AddNewAlgorithm_Load(object sender, EventArgs e)
        {
            lbAddedParameters.SelectedIndexChanged += SelectedIndexChangedEventHandler;
        }

        private void SelectedIndexChangedEventHandler(object sender, EventArgs e)
        {
            int index = lbAddedParameters.SelectedIndex;
            tbParamName.Text = localLst[index].paramName;
            tbDescription.Text = localLst[index].description;
            tbAcceptableValues.Text = localLst[index].values;
        }

        private void ClearParamTBoxes()
        {
            tbParamName.Text = string.Empty;
            tbDescription.Text = string.Empty;
            tbAcceptableValues.Text = string.Empty;
        }

        private void ClearAll()
        {
            ClearParamTBoxes();
            tbFileName.Text = string.Empty;
            tbDllPath.Text = string.Empty;
            tbAlgName.Text = string.Empty;
            tbExePath.Text = string.Empty;
            lbAddedParameters.Items.Clear();
        }

        private void btnExePath_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = openFileDialog1.SafeFileName;
                string filePath = openFileDialog1.FileName;
                if (fileName.EndsWith(ExeExt))
                {
                    tbExePath.Text = filePath;
                }
                else
                {
                    MessageBox.Show(Resources.ADDALGDescr_InvalidExeExtText, 
                        Resources.ADDALGDescr_InvalidLibraryExtTitle, MessageBoxButtons.OK);
                }
            }
        }
    }
}
